耍杂技的牛

题目 耍杂技的牛

image-0e2b76d8

思路分析

能想到体重大的放下面 然后可承受重量大的放下面

但没敢想成是 两个之和更大的放下面……

代码实现

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 50010;

struct node{

    int w,s;

    bool operator<(const node& other)const{

        return w+s<other.w+other.s;

    }

}a[N];

int n;

ll sum=0;

int ans=0;

int main(){

    cin>>n;

    for(int i=0;i<n;i++)

        cin>>a[i].w>>a[i].s;

    sort(a,a+n);

    int res=-1e9;

    int sum=0;

    for(int i=0;i<n;i++){

        res=max(res,sum-a[i].s);

        sum+=a[i].w;

    }

    cout<<res<<endl;

    return 0;

}

同类题型

视频讲解


⬅️ 最小消耗 🏠 00-刷题理模型 ➡️ 重新排序